package 排序;

import java.util.Arrays;

public class Test2 {
    public static void main(String[] args) {
        int count=0;
        int[] arr={5,3,2,1,4};
        int n =arr.length-1;
        for (int j = 0; j < arr.length-1; j++) {
            int last =0;

            for (int i = 0; i < n; i++) {

                if (arr[i]>arr[i+1]) {
                    int temp = arr[i];
                    arr[i] = arr[i+1];
                    arr[i+1] = temp;
                    last = i;
                    count++;
                }

            }
            n = last;
            if (n==0){
                break;
            }


        }
        System.out.println("一共比较的次数："+ count);
        System.out.println(Arrays.toString( arr));
    }
}
